package com.yehun.service.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.yehun.dao.TestDao;
import com.yehun.entity.PagerEntity;
import com.yehun.entity.Test;
import com.yehun.service.TestService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.List;

@Service
public class TestServiceImpl implements TestService {

    private final static Logger logger = LoggerFactory.getLogger(TestServiceImpl.class);

    @Resource
    private TestDao testDao;

    @Override
    public List<Test> list() {
        return testDao.list();
    }

    @Override
    public PagerEntity<Test> list(int pageIndex, int pageSize) {
        PagerEntity<Test> pager = new PagerEntity<>();
        Page page = PageHelper.startPage(pageIndex, pageSize);
        pager.setData(testDao.list());
        pager.setPageIndex(pageIndex);
        pager.setPageSize(pageSize);
        pager.setTotal(page.getTotal());
        return pager;
    }

    @Override
    public Test get(int id) {
        return testDao.get(id);
    }

    @Override
    public int save(Test test) {
        return testDao.save(test);
    }

    @Override
    @Transactional
    public int batchSave(Test[] tests) {
        int row = 0;
        if (tests != null) {
            for (Test test: tests) {
                row += testDao.save(test);
            }
        }
        return row;
    }

    @Override
    public int update(Test test) {
        return testDao.update(test);
    }

    @Override
    @Transactional
    public int batchUpdate(Test[] tests) {
        int row = 0;
        if (tests != null) {
            for (Test test: tests) {
                row += testDao.update(test);
            }
        }
        return row;
    }

    @Override
    public int remove(int id) {
        return testDao.remove(new int[] { id });
    }

    @Override
    public int batchRemove(int[] ids) {
        return testDao.remove(ids);
    }
}
